package com.sale.system.service.impl;

import com.sale.common.core.exception.GlobalException;
import com.sale.common.satoken.utils.LoginHelper;
import com.sale.system.domain.SysWorkbench;
import com.sale.system.domain.dto.PageUrlDto;
import com.sale.system.domain.dto.SysMenusDto;
import com.sale.system.domain.dto.SysWorkbenchDto;
import com.sale.system.domain.vo.SysWorkbenchVo;
import com.sale.system.mapper.SysMenuMapper;
import com.sale.system.mapper.SysWorkbenchMapper;
import com.sale.system.service.ISysMenuService;
import com.sale.system.service.SysWorkbenchService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 快捷功能实现层
 */
@RequiredArgsConstructor
@Service
public class SysWorkbenchServiceImpl implements SysWorkbenchService {


    private final SysWorkbenchMapper sysWorkbenchMapper;

    private final SysMenuMapper menuMapper;

    private final ISysMenuService sysMenuService;

    /**
     * 获取登录用户快捷功能列表
     *
     * @param sysWorkbenchVo
     * @return
     */
    @Override
    public List<SysWorkbenchDto> getList(SysWorkbenchVo sysWorkbenchVo) {
        sysWorkbenchVo.setUserId(LoginHelper.getUserId());
        List<SysWorkbenchDto> sysWorkbenchList = sysWorkbenchMapper.getSysWorkbenchList(sysWorkbenchVo);
        for (SysWorkbenchDto entity : sysWorkbenchList) {
            List<String> idStr = new ArrayList<>();
            idStr.add(String.valueOf(entity.getMenuId()));
            List<SysMenusDto> idByMenuInfos = sysMenuService.getMenuDataByIdStr(idStr);
//            entity.setMenuUrl(idByMenuInfos.get(0).getPath());
            entity.setIcon(idByMenuInfos.get(0).getIcon());
        }
        return sysWorkbenchList;
    }

    /**
     * 新增/修改
     *
     * @param sysWorkbench
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean addOrUpdate(SysWorkbenchVo sysWorkbench) {
        List<String> idStr = sysWorkbench.getIdStr();
        if (idStr.size() < 1) {
            throw new GlobalException("请至少选择一条数据");
        }
        SysWorkbenchVo sysWorkbenchVo = new SysWorkbenchVo();
        sysWorkbenchVo.setUserId(LoginHelper.getUserId());
        List<SysWorkbenchDto> sysWorkbenchList = sysWorkbenchMapper.getSysWorkbenchList(sysWorkbenchVo);
        //没有走新增 有走修改
        if (sysWorkbenchList.size() < 1) {

            //获取菜单名称及对应路由
            List<SysMenusDto> idByMenuInfos = sysMenuService.getMenuDataByIdStr(idStr);

            List<SysWorkbench> sysWorkbenchVoList = new ArrayList<>();

            for (SysMenusDto entity : idByMenuInfos) {
                SysWorkbench result = new SysWorkbench();
                result.setMenuName(entity.getMenuName());
                //根据菜单id获取页面路由
                List<PageUrlDto> pageUrlList = menuMapper.getPageUrlList(entity.getMenuId());
                StringBuilder stringBuilder = new StringBuilder();
                for (PageUrlDto entityUrl : pageUrlList) {
                    stringBuilder.append(entityUrl.getPageUrl()).append("/");
                }
                StringBuilder stringBuilder1 = stringBuilder.deleteCharAt(stringBuilder.length() - 1);
                result.setMenuUrl("/" + stringBuilder1);
                result.setMenuId(entity.getMenuId());
                result.setUserId(LoginHelper.getUserId());
                sysWorkbenchVoList.add(result);
            }
            return sysWorkbenchMapper.insertBatch(sysWorkbenchVoList);
        } else {
            //删除之前的数据
            int i = sysWorkbenchMapper.deleteData(sysWorkbenchVo);
            if (i > 0) {
                //获取菜单名称及对应路由
                List<SysMenusDto> idByMenuInfos = sysMenuService.getMenuDataByIdStr(idStr);

                List<SysWorkbench> sysWorkbenchVoList = new ArrayList<>();

                for (SysMenusDto entity : idByMenuInfos) {
                    SysWorkbench result = new SysWorkbench();
                    result.setMenuName(entity.getMenuName());
                    //根据菜单id获取页面路由
                    List<PageUrlDto> pageUrlList = menuMapper.getPageUrlList(entity.getMenuId());
                    StringBuilder stringBuilder = new StringBuilder();
                    for (PageUrlDto entityUrl : pageUrlList) {
                        stringBuilder.append(entityUrl.getPageUrl()).append("/");
                    }
                    StringBuilder stringBuilder1 = stringBuilder.deleteCharAt(stringBuilder.length() - 1);
                    result.setMenuUrl("/" + stringBuilder1);
                    result.setMenuId(entity.getMenuId());
                    result.setUserId(LoginHelper.getUserId());
                    sysWorkbenchVoList.add(result);
                }
                return sysWorkbenchMapper.insertBatch(sysWorkbenchVoList);
            }
        }
        return false;
    }
}
